//package com.ruoyi.system.quartz;
//
//import cn.hutool.core.collection.IterUtil;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.util.CharsetUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.json.JSONUtil;
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.ruoyi.system.domain.HUAWEI.EmsBarcodeAttribute;
//import com.ruoyi.system.domain.HUAWEI.EmsBarcodeMaterial;
//import com.ruoyi.system.domain.HUAWEI.EmsBarcodeRelation;
//import com.ruoyi.system.domain.HUAWEI.TBtpMtlCurriculumViateTi;
//import com.ruoyi.system.domain.MES.*;
//import com.ruoyi.system.domain.MqExceptionData;
//import com.ruoyi.system.domain.function.ProdOrder;
//import com.ruoyi.system.domain.function.ProdPlan;
//import com.ruoyi.system.enums.MaterialEnum;
//import com.ruoyi.system.enums.MesEnum;
//import com.ruoyi.system.mapper.HUAWEI.EmsBarcodeAttributeMapper;
//import com.ruoyi.system.mapper.HUAWEI.EmsBarcodeMaterialMapper;
//import com.ruoyi.system.mapper.HUAWEI.EmsBarcodeRelationMapper;
//import com.ruoyi.system.mapper.HUAWEI.TBtpMtlCurriculumViateTiMapper;
//import com.ruoyi.system.mapper.MES.DataCollectionInfoMapper;
//import com.ruoyi.system.mapper.MES.DataCollectionMapper;
//import com.ruoyi.system.mapper.MqExceptionDataMapper;
//import com.ruoyi.system.mapper.ProdOrderMapper;
//import com.ruoyi.system.mapper.ProdPlanMapper;
//import com.ruoyi.system.service.IBaseSerialNumberService;
//import com.ruoyi.system.utils.InterfaceLogUtil.InterfaceLogUtil;
//import com.ruoyi.system.utils.rabbitMq.direct.DirectReceiver;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
///**
// * @description: mq消费异常数据重传定时任务
// * @author: zhengxiong.jiang
// * @createDate: 2024/1/18
// * @version: 1.0
// */
//@Component("MqTask")
//public class MqExceptionQuartz {
//
//    @Autowired
//    private MqExceptionDataMapper mqExceptionDataMapper;
//    @Autowired
//    private DataCollectionMapper dataCollectionMapper;
//    @Autowired
//    private DataCollectionInfoMapper dataCollectionInfoMapper;
//    @Autowired
//    private IBaseSerialNumberService iBaseSerialNumberService;
//    @Autowired
//    private DirectReceiver directReceiver;
//    @Autowired
//    private ProdOrderMapper prodOrderMapper;
//    @Autowired
//    private EmsBarcodeAttributeMapper emsBarcodeAttributeMapper;
//    @Autowired
//    private InterfaceLogUtil interfaceLogUtil;
//    @Autowired
//    private TBtpMtlCurriculumViateTiMapper tbmapper;
//    @Autowired
//    private EmsBarcodeRelationMapper emsBarcodeRelationMapper;
//    @Autowired
//    private EmsBarcodeMaterialMapper emsBarcodeMaterialMapper;
//    @Autowired
//    private ProdPlanMapper prodPlanMapper;
//
//    /**
//     * mq消费数据重传华为
//     */
//    public void againSendHuawei() {
//        List<MqExceptionData> mqExceptionData = mqExceptionDataMapper.selectList(new QueryWrapper<>());
//        if(IterUtil.isNotEmpty(mqExceptionData)){
//            mqExceptionData.forEach(i -> {
//                if (i.getMqType().equals(InterfaceLogUtil.INTERFACENAME_EXCEPTION_GJWLBD)) {//关键物料绑定
//                    this.againGjwl(i.getContentStr());
//                } else if (i.getMqType().equals(InterfaceLogUtil.INTERFACENAME_EXCEPTION_GZXF)) {//过站信息消费异常
//                    this.againGzxx(i.getContentStr());
//                }
//                //重传之后进行数据删除
//                mqExceptionDataMapper.deleteById(i.getId());
//            });
//        }
//    }
//
//    /**
//     * 关键物料绑定重传
//     */
//    public void againGjwl(String contentStr) {
//        //开始消费的时间
//        Date startDate = new Date();
//
//        try {
//            Map maps = (Map) JSON.parse(contentStr);
//            Object body = maps.get("Body");
//            String jsonDatas = JSONUtil.toJsonStr(body);
//            //你的业务逻辑
//            KeymatBindDTO keymatBindDTO = JSONUtil.toBean(jsonDatas, KeymatBindDTO.class);
//            KeymatBind keymatBind = new KeymatBind();
//            BeanUtils.copyProperties(keymatBindDTO, keymatBind);
//            keymatBind.setCreateBy("admin");
//            keymatBind.setCreateTime(new Date());
//            List<MaterialDatasDTO> materialList = keymatBindDTO.getMaterialList();
//            if (IterUtil.isNotEmpty(materialList)) {
//                materialList.stream().forEach(i -> {
//
//                    //根据物料编码筛选华为物料，进行区分sn，09、psn 逻辑取消
//                    if (i.getFMaterialType().equals(MaterialEnum.B_INW_BATCH_PSN.getDesc())) {//psn/09
//                        EmsBarcodeMaterial barcodeMaterial = new EmsBarcodeMaterial();
//                        ProdOrder prodOrder = prodOrderMapper.selectOne(new QueryWrapper<ProdOrder>().eq("order_code", keymatBindDTO.getFPlanNo()));
//                        String taskTon = prodOrder.getTaskTon();
//
//                        barcodeMaterial.setPo(taskTon);
//                        barcodeMaterial.setBarCode(keymatBindDTO.getFNumber());
//                        barcodeMaterial.setItemCode(i.getFMaterielCode());
//                        List<TBtpMtlCurriculumViateTi> list = tbmapper.selectList(new QueryWrapper<TBtpMtlCurriculumViateTi>()
//                                .eq("item_code", i.getFMaterielCode())
//                                .eq("barcode", i.getFMaterielBarcode())
//                                .eq("process_name", "任务令发料")
//                                .eq("task_no", taskTon)
//                                .orderByDesc("created_date")
//                        );
//
//                        if (IterUtil.isNotEmpty(list)) {
//                            barcodeMaterial.setDateCode(list.get(0).getDateCode());
//                            barcodeMaterial.setLotCode(list.get(0).getLotCode());
//                        }
//
//                        //通过工单找到对应物料清单的数量
//                        ProdPlan prodPlan = prodPlanMapper.selectOne(new QueryWrapper<ProdPlan>().eq("plan_code", prodOrder.getPlanCode()));
//                        barcodeMaterial.setQuantity(Double.valueOf(i.getPatchCount()));
//                        barcodeMaterial.setVendorId("22827731");//华为默认
//                        barcodeMaterial.setOrgId(157);//华为默认
//                        barcodeMaterial.setCreatedDate(i.getFDate());
//                        barcodeMaterial.setCreatedBy("马鞍山工厂");
//                        barcodeMaterial.setUploadFlag(0);
//                        String fMaterielBarcode = i.getFMaterielBarcode();
//
//                        //todo 临时逻辑 后续可能会删除此逻辑
//                        if (StrUtil.isNotEmpty(fMaterielBarcode) && fMaterielBarcode.contains("#")) {
//                            String[] split = fMaterielBarcode.split("#");
//                            if (split.length > 0) {
//                                fMaterielBarcode = split[0];//split[1]date_code split[2]lot_code
//                            }
//                            if (split.length > 1) {
//                                barcodeMaterial.setDateCode(split[1]);
//                            }
//                            if (split.length > 2) {
//                                barcodeMaterial.setLotCode(split[2]);
//                            }
//                        }
//                        List<TBtpMtlCurriculumViateTi> list2 = tbmapper.selectList(new QueryWrapper<TBtpMtlCurriculumViateTi>()
//                                .eq("barcode", i.getFMaterielBarcode())
//                                .orderByDesc("created_date")
//                        );
//                        if (IterUtil.isNotEmpty(list2)) {
//                            barcodeMaterial.setSupplier(list2.get(0).getMtlVendorName());
//                        }
//
//                        barcodeMaterial.setSegment1(fMaterielBarcode);
//                        barcodeMaterial.setSegment2("N");
//                        barcodeMaterial.setSegment4(prodPlan.getTaskTon());
//                        barcodeMaterial.setSegment13(i.getFMaterielBarcode());
//                        barcodeMaterial.setSegment14("40033");
//                        barcodeMaterial.setSegment15("EMS");
//                        barcodeMaterial.setPtionSegment(i.getFMaterielCode().substring(0, 2) + "22827731");
//                        barcodeMaterial.setBarItemCode(keymatBind.getFProdCodeNo());
//                        barcodeMaterial.setEmsTransId(iBaseSerialNumberService.getSerialNumber("H4", 16));
//                        emsBarcodeMaterialMapper.insert(barcodeMaterial);
//                    } else if (i.getFMaterialType().equals(MaterialEnum.B_INW_BATCH_SN.getDesc())) {
//                        String h4 = iBaseSerialNumberService.getSerialNumber("H4", 8);
//                        ProdOrder prodOrder = prodOrderMapper.selectOne(new QueryWrapper<ProdOrder>().eq("order_code", keymatBindDTO.getFPlanNo()));
//                        String taskTon = prodOrder.getTaskTon();
//                        String mtlVendorName = "";
//                        List<TBtpMtlCurriculumViateTi> tis = tbmapper.selectList(new QueryWrapper<TBtpMtlCurriculumViateTi>()
//                                .eq("task_no", taskTon)
//                                .eq("item_code", i.getFMaterielCode()));
//                        if (IterUtil.isNotEmpty(tis)) {
//                            mtlVendorName = tis.get(0).getMtlVendorName();
//                        }
//
//                        // todo 测试先注释后续放开 进行华为父子条码追溯信息接口发送
//                        String res = directReceiver.sendHuaweitmzs(i, keymatBind, h4, mtlVendorName);
//
//                        //本地数据存储
//                        EmsBarcodeRelation emsBarcodeRelation = new EmsBarcodeRelation(i, keymatBind);
//                        emsBarcodeRelation.setPo(taskTon);
//                        emsBarcodeRelation.setSegment3(taskTon);
//                        emsBarcodeRelation.setEmsTransId(h4);
//                        emsBarcodeRelation.setSupplier(mtlVendorName);
//                        // todo 测试先注释后续放开
//                        emsBarcodeRelation.setRes(res);
//                        emsBarcodeRelationMapper.insert(emsBarcodeRelation);
//                    }
//
//                });
//            }
//        } catch (Exception e) {
//
//            //消费异常进行日志记录
//            interfaceLogUtil.insertLog(contentStr,
//                    startDate, new Date(), InterfaceLogUtil.DIR_MES_MOM,
//                    InterfaceLogUtil.TYPE_RABBITMQ, e.getMessage(),
//                    InterfaceLogUtil.INTERFACENAME_EXCEPTION_GJWLBD
//            );
//
//            //如果异常为网络异常的类型，将数据保存，用定时任务方式后续定期重传
//            if (e.getMessage().equals(InterfaceLogUtil.TYPE_EXCEPTION_WL)) {
//                directReceiver.saveExceptionWl(contentStr, InterfaceLogUtil.INTERFACENAME_EXCEPTION_GJWLBD, startDate);
//            }
//
//
//        }
//    }
//
//    /**
//     * 过站信息重传
//     */
//    public void againGzxx(String contentStr) {
//        Date startDate = new Date();
//        try {
//            Map maps = (Map) JSON.parse(contentStr);
//            Object body = maps.get("Body");
//            String jsonDatas = JSONUtil.toJsonStr(body);
//            //你的业务逻辑
//            if (StrUtil.isNotEmpty(jsonDatas)) {
//                DataCollection dataCollection = JSONUtil.toBean(jsonDatas, DataCollection.class);
//                dataCollection.setCreateBy("admin");
//                dataCollection.setCreateTime(new Date());
//                dataCollectionMapper.insert(dataCollection);
//                List<DataCollectionInfo> dataList = dataCollection.getDataList();
//
//                if (MesEnum.OP010_1.getCode().equals(dataCollection.getFProcessID())) {
//                    for (int i = 0; i < dataList.size(); i++) {
//                        dataList.get(i).setCollectionId(dataCollection.getCollectionId());
//                        dataList.get(i).setCreateBy("admin");
//                        dataList.get(i).setCreateTime(new Date());
//                        dataCollectionInfoMapper.insert(dataList.get(i));
//                    }
//                    String h4 = iBaseSerialNumberService.getSerialNumber("H4", 10);
//                    //todo 测试先注释后续放开 进行华为条码属性回传
//                    String res = directReceiver.sendAttribute(dataCollection, h4);
//
//                    //本地数据存储
//                    ProdOrder prodOrder = prodOrderMapper.selectOne(new QueryWrapper<ProdOrder>().eq("order_code", dataCollection.getFPlanNo()));
//                    EmsBarcodeAttribute emsBarcodeAttribute = new EmsBarcodeAttribute(dataCollection);
//                    emsBarcodeAttribute.setEmsTransId(h4);
//                    emsBarcodeAttribute.setPo(prodOrder.getTaskTon());
//                    emsBarcodeAttribute.setSegment5(prodOrder.getTaskTon());
//                    emsBarcodeAttribute.setCreatedBy(dataCollection.getFAccount() + "/" + dataCollection.getFUserName());
//                    //todo 测试先注释后续放开
//                    emsBarcodeAttribute.setRes(res);
//                    emsBarcodeAttributeMapper.insert(emsBarcodeAttribute);
//                } else if (MesEnum.OP040.getCode().equals(dataCollection.getFProcessID())) {
//
//                    //进行客户条码回传
//                    directReceiver.sendCustomerSNAndHWSNRela(dataCollection,contentStr);
//
//                }
//            }
//
//        } catch (Exception e) {
//            //如果异常为网络异常的类型，将数据保存，用定时任务方式后续定期重传
//            if (e.getMessage().equals(InterfaceLogUtil.TYPE_EXCEPTION_WL)) {
//                directReceiver.saveExceptionWl(contentStr, InterfaceLogUtil.INTERFACENAME_EXCEPTION_GZXF, startDate);
//            }
//
//        }
//    }
//
//
//}
